package com.woniuxy.edubureau.controller;

import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.SchoolInfo;
import com.woniuxy.commons.exception.BzeducationException;
import com.woniuxy.edubureau.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: luojiwei
 * @date: 2020/7/17 10:27
 * @description: 创建学校的controller
 */
@RestController
@RequestMapping("/school")
public class SchoolController extends BzeducationException {
    @Autowired
    private SchoolService schoolService;

    //创建学校
    @PostMapping("/create")
    public ResponseEntity<Void> createSchoolInfo(SchoolInfo schoolInfo){
        ResponseEntity<Void> responseEntity=new ResponseEntity<>();
        Integer maxid=schoolService.getMaxId();
        System.out.println(maxid);
        String snumber="";
        if (maxid==null){
            snumber="1000";
            schoolInfo.setSnumber(snumber);
        }else {
            snumber=(Integer.parseInt(schoolService.getSnumber(maxid))+1)+"";
            schoolInfo.setSnumber(snumber);
        }
        int res=schoolService.createSchoolInfo(schoolInfo);
        if (res>0) {
            responseEntity.setMsg("创建学校成功");
            responseEntity.setCode(ResponseEntity.SUCCESS);
        }
        return responseEntity;
    }

    //分页查询展示
    @GetMapping("/all")
    public ResponseEntity<List<SchoolInfo>> getSchoolInfos(int page, int limit){
        ResponseEntity<List<SchoolInfo>> responseEntity=new ResponseEntity<>();
        int p=(page-1)*limit;
        responseEntity.setCount(schoolService.getDataNums());
        responseEntity.setData(schoolService.getAll(p,limit));
        return responseEntity;
    }

    //获取所有学校信息
    @GetMapping("/alls")
    public ResponseEntity<List<SchoolInfo>> getAlls(){
        ResponseEntity<List<SchoolInfo>> responseEntity=new ResponseEntity<>();
        responseEntity.setData(schoolService.getAllSchoolInfos());
        return responseEntity;
    }
}
